package com.framework.dubbo.producer.utils;

import cn.hutool.core.util.StrUtil;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.fit.pdfdom.PDFDomTreeConfig;

import java.io.*;

/**
 * 描述：
 * java工具类转
 * @author xianggj
 * @Date 2021/9/22 10:39
 **/
public class PdfBoxUtils {

    /**
     * java转换
     * @param pdfPath
     * @param htmlPath
     */
    public static void pdf2html(String pdfPath, String htmlPath) {

        try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(new File(htmlPath)),"UTF-8"))){
            File pdfFile = new File(pdfPath);
            PDDocument document = Loader.loadPDF(pdfFile);
            PDFDomTreeConfig domTreeConfig = PDFDomTreeConfig.createDefaultConfig();
            String dir = StrUtil.subBefore(htmlPath, ".", true);
            domTreeConfig.setImageHandler(PDFDomTreeConfig.saveToDirectory(new File(dir)));
            domTreeConfig.setFontHandler(PDFDomTreeConfig.saveToDirectory(new File(dir)));
            MYPDFDomTree parser = new MYPDFDomTree(domTreeConfig);
            parser.writeText(document, out);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * java转换
     * @param pdfPath
     */
    public static String pdf2Str(String pdfPath) {

        try {
            InputStream is = new FileInputStream(pdfPath);
            return pdf2Str(is);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * java转换
     * @param is
     */
    public static String pdf2Str(InputStream is) {

        PDDocument document = null;
        try {
            document = Loader.loadPDF(is);
            PDFTextStripper stripper = new PDFTextStripper();
            String str = stripper.getText(document).trim();
            return str;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != document) {
                try {
                    document.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
//        File files = new File("D:\\sinux\\pdf\\hn_data");
//        for ( File file :files.listFiles()) {
//
//            String name = file.getName();
//            String fileName = StrUtil.subBefore(name, ".", true);
//            pdf2html(file.getPath(),
//                    "D:\\sinux\\pdf\\html\\"+fileName+".html");
//        };
        pdf2html("D:\\sinux\\pdf\\JFusion.pdf",
                "D:\\sinux\\pdf\\html\\JFusion.html");
//        String path = PdfBoxUtils.class.getResource("/").getPath();
//        System.out.println(path);
//        System.out.println(StrUtils.formatStr(pdf2Str(path + File.separator + "0349fd0fdb4d4e1788ecacc2104c6856.pdf")));
    }

    /**
     * 将文件转换为byte数组
     **/
    private byte[] getBytes(String filePath){
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }
}
